<?php
/**
 * PHP Daycare
 * Has all of the functions for the admin panel involving classes
 * @author Jason Butz
 * @version 0.0
 * @package PHPDaycare
 */

include_once('config.php');
include_once('functions.php');

$mode = $_GET['mode'];
session_start();
// Our user must be logged in & an admin to view things
if(loggedInAsAdmin() == 1)
{
	switch($mode)
	{
		case 'add':
				if(isset($_GET['i']))
				{
					// We are inserting a new class into the DB
					$name = mysql_escape_string($_GET['name']);
					$location = mysql_escape_string($_GET['location']);
					$desc = mysql_escape_string($_GET['desc']);

					// Lets add it!
					$sql = "INSERT INTO `class` 
					(`name`, `location`, `desc`) 
					VALUES 
					('$name', '$location', '$desc');";
					$r = $db->query($sql);
					if($r)
					{
						echo "<p>The new class was added successfully.</p>";
					}
					else
					{
						echo "<p>An unknown error occurred and the class was not added.</p>";
					}
							
				}
				echo "<p>Please enter the new class's information below.</p>";
				echo "<form name=\"addclass\">
				<p><label>Name:<br /><input type='text' name='name' size='25' tabindex='1'></label></p>
				<p><label>Location:<br /><input type='text' name='location' size='15' tabindex='2'></label></p>
				<p><label>Description:<br /><textarea name='desc' cols='45' rows='3' tabindex='3'></textarea></label></p>
				<p><input type='button' tabindex='4' value='Add Class' onClick=\"loadAdmininfo('classes.php', 'mode=add&i=1&name='+encodeURIComponent(document.addclass.name.value)+'&desc='+encodeURIComponent(document.addclass.desc.value)+'&location='+encodeURIComponent(document.addclass.location.value))\"/></p>";
			break;
		case 'modify':
			$cm =  'v';
			if(isset($_GET['cm']))
			{
				$cm = $_GET['cm'];
			}
			switch($cm)
			{
				case 'v': // Default action - view
					// We need a list of all the classes.
					$sql = "SELECT * FROM class;";
					$r = $db->query($sql);
					echo "
					<p>Please select the class you wish to modify.</p>
					<table cellspacing='1' class='tablesorter' width='100%'>
					<thead>
						<tr>
							<th class=\"{sorter: 'text'}\">Name</th>
							<th class=\"{sorter: 'text'}\">Location</th>
							<th class=\"{sorter: 'text'}\">Description</th>
							<th class=\"{sorter: 'text'}\">Modify</th>
						</tr>
					</thead>
					<tbody>";
					while($row = mysql_fetch_row($r))
					{
						echo "<tr>
									<td>" . $row[1] . "</td>
									<td>" . $row[2] . "</td>
									<td>" . $row[3] . "</td>
									<td><a href= \"#\" onClick=\"loadAdmininfo('classes.php', 'mode=modify&cm=m&id=".$row[0]."')\">Modify</a></td>
							</tr>";
					}
					echo "</tbody></table>";
					$temp->tablePager();
					echo "<script type=\"text/javascript\">loadsorter()</script><p>&nbsp;</p>";
					break;
				case 'm': // Modifcation page for class
					$id = $_GET['id'];
					$sql = "SELECT * FROM class WHERE ID=$id;";
					$r = $db->query($sql);
					$row = mysql_fetch_row($r);
					echo "<p>Please enter the new class's information below.</p>";
					echo "<form name=\"addclass\">
					<p><label>Name:<br /><input type='text' name='name' size='25' tabindex='1' value='$row[1]'></label></p>
					<p><label>Location:<br /><input type='text' name='location' size='15' tabindex='2' value='$row[2]'></label></p>
					<p><label>Description:<br /><textarea name='desc' cols='45' rows='3' tabindex='3'>$row[3]</textarea></label></p>
					<p><input type='button' tabindex='4' value='Modify Class' onClick=\"loadAdmininfo('classes.php', 'mode=modify&cm=u&id=$id&name='+encodeURIComponent(document.addclass.name.value)+'&desc='+encodeURIComponent(document.addclass.desc.value)+'&location='+encodeURIComponent(document.addclass.location.value))\"/></p>";
					break;
				case 'u': // Update the modifications then set page to view workers.
					// We are update a worker in the DB
					$id = mysql_escape_string($_GET['id']);
					$name = mysql_escape_string($_GET['name']);
					$location = mysql_escape_string($_GET['location']);
					$desc = mysql_escape_string($_GET['desc']);
					$sql = "UPDATE class SET `name` = '$name', `location` = '$location', `desc` = '$desc' WHERE `ID` = '$id';";
					$r = $db->query($sql);
					if($r)
					{
						echo "<p>The class was modified successfully.</p>";
					}
					else
					{
						echo "<p>An unknown error occurred and the class was not updated.</p>";
					}
					break;
			}
			break;
		case 'delete':
			// We may be deleting a worker
			if(isset($_GET['id']))
			{
				$id = $_GET['id'];
				// Now we delete
				$sql = "DELETE FROM class WHERE ID=$id";
				$r = $db->query($sql);
				if($r)
				{
					echo "<p>The class was deleted.</p>";
				}
				else
				{
					echo "<p>An error occurred. The class was not deleted.</p>";
				}
			}
			// We need a list of all the workers.
			$sql = "SELECT * FROM class";
			$r = $db->query($sql);
			echo "
			<p>Please select the class you wish to delete. This operation CAN NOT be undone. 
			This will also delete all attendance records for this class and checkout the children who are currently checked in to the class.</p>
			<table cellspacing='1' class='tablesorter' width='500px'>
			<thead>
				<tr>
					<th class=\"{sorter: 'text'}\">Name</th>
					<th class=\"{sorter: 'text'}\">Location</th>
					<th class=\"{sorter: 'text'}\">Description</th>
					<th class=\"{sorter: 'text'}\">Delete</th>
				</tr>
			</thead>
			<tbody>";
			while($row = mysql_fetch_row($r))
			{
				echo "<tr>
							<td>" . $row[1] . "</td>
							<td>" . $row[2] . "</td>
							<td>" . $row[3] . "</td>
							<td><a href= \"#\" onClick=\"loadAdmininfo('classes.php', 'mode=delete&id=".$row[0]."')\">Delete</a></td>
					</tr>";
			}
			echo "</tbody></table>";
			$temp->tablePager();
			echo "<script type=\"text/javascript\">loadsorter()</script><p>&nbsp;</p>";
			break;
		case 'attend':
			if(isset($_GET['id']))
			{
				$id = $_GET['id'];
				$sql = "SELECT * FROM `attendance` WHERE class_id = $id";
				$r = $db->query($sql);
				
				// We need to have a mapping of IDs to names. So lets do that in functions.php
				$children = mapChildren();
				$classes = mapClasses();
				$adults = mapAdults();
				
				echo "
				<table cellspacing='1' class='tablesorter' width='100%'>
				<thead>
					<tr>
						<th class=\"{sorter: 'text'}\">Child</th>
						<th class=\"{sorter: 'text'}\">Class</th>
						<th class=\"{sorter: 'text'}\">Adult - Checkin</th>
						<th class=\"{sorter: 'text'}\">Time - In</th>
						<th class=\"{sorter: 'text'}\">Adult - Checkout</th>
						<th class=\"{sorter: 'text'}\">Time - Out</th>
					</tr>
				</thead>
				<tbody>";
				while($row = mysql_fetch_row($r))
				{
					// It is possible that there is no out time yet.
					// we need to allow for this possibility.
					if($row[6] == NULL)
					{
						$outtime = "&nbsp;";
					}
					else
					{
						$outtime = date("m/d/y h:i A", strtotime($row[6]));
					}
					echo "<tr>
								<td>" . $children[ $row[1] ] . "</td>
								<td>" . $classes [ $row[2] ] . "</td>
								<td>" . $adults [$row[3] ] . "</td>
								<td>" . date("m/d/y h:i A", strtotime($row[5])) . "</td>
								<td>" . $adults[ $row[4] ] . "</td>
								<td>" . $outtime . "</td>
						</tr>";
				}
				echo "</tbody></table>";
				$temp->tablePager();
				echo "<script type=\"text/javascript\">loadsorterAttend();</script><p>&nbsp;</p>";
			}
			else
			{
				$sql = "SELECT * FROM class";
				$r = $db->query($sql);
				echo "
				<p>Please select the class you wish view the attendance for.</p>
				<table cellspacing='1' class='tablesorter' width='500px'>
				<thead>
					<tr>
						<th class=\"{sorter: 'text'}\">Name</th>
						<th class=\"{sorter: 'text'}\">Location</th>
						<th class=\"{sorter: 'text'}\">Description</th>
						<th class=\"{sorter: 'text'}\">View</th>
					</tr>
				</thead>
				<tbody>";
				while($row = mysql_fetch_row($r))
				{
					echo "<tr>
								<td>" . $row[1] . "</td>
								<td>" . $row[2] . "</td>
								<td>" . $row[3] . "</td>
								<td><a href= \"#\" onClick=\"loadAdmininfo('classes.php', 'mode=attend&id=".$row[0]."')\">View</a></td>
						</tr>";
				}
				echo "</tbody></table>";
				$temp->tablePager();
				echo "<script type=\"text/javascript\">loadsorter()</script><p>&nbsp;</p>";
			}
			break;
	}
}
else
{
	// not logged in or not admin
	echo "<p>Error: You are not logged in, or are not an administrator. 
	Please log in as an administrator if you wish to access this area. 
	You may login <a href='index.php'>here</a>.</p>";
}
?>
